using FreeSql.DataAnnotations;

namespace SumerCoreDevOps.Domain.Entities;

/// <summary>
/// 节点操作日志实体
/// </summary>
[Table(Name = "node_operation_logs")]
[Index("idx_node_id_time", "NodeId,CreatedAt DESC")]
[Index("idx_operation_type", "OperationType")]
[Index("idx_created_at", "CreatedAt DESC")]
public class NodeOperationLog
{
    /// <summary>
    /// 日志 ID
    /// </summary>
    [Column(IsPrimary = true, IsIdentity = true)]
    public long Id { get; set; }

    /// <summary>
    /// 节点 ID
    /// </summary>
    [Column(StringLength = 100, IsNullable = false)]
    public string NodeId { get; set; } = null!;

    /// <summary>
    /// 节点名称（冗余字段，方便查询）
    /// </summary>
    [Column(StringLength = 200)]
    public string? NodeName { get; set; }

    /// <summary>
    /// 操作类型：
    /// - NodeRegister: 节点注册
    /// - NodeUnregister: 节点注销
    /// - Heartbeat: 心跳
    /// - TaskReceive: 接收任务
    /// - TaskStart: 开始执行任务
    /// - TaskProgress: 任务进度更新
    /// - TaskComplete: 任务完成
    /// - TaskFail: 任务失败
    /// - LogStream: 日志流
    /// - CleanupStart: 开始清理
    /// - CleanupComplete: 清理完成
    /// - ConfigUpdate: 配置更新
    /// - Error: 错误
    /// </summary>
    [Column(StringLength = 50, IsNullable = false)]
    public string OperationType { get; set; } = null!;

    /// <summary>
    /// 关联的任务 ID（如果操作与任务相关）
    /// </summary>
    [Column(StringLength = 100)]
    public string? TaskId { get; set; }

    /// <summary>
    /// 关联的流水线实例 ID
    /// </summary>
    public long? PipelineInstanceId { get; set; }

    /// <summary>
    /// 操作状态：Success, Failed, InProgress
    /// </summary>
    [Column(StringLength = 50, IsNullable = false)]
    public string Status { get; set; } = "InProgress";

    /// <summary>
    /// 操作描述/消息
    /// </summary>
    [Column(StringLength = 1000)]
    public string? Message { get; set; }

    /// <summary>
    /// 详细信息（JSON 格式）
    /// 可以存储更多结构化数据，如负载信息、错误堆栈等
    /// </summary>
    [Column(StringLength = -1)]
    public string? Details { get; set; }

    /// <summary>
    /// 错误消息（如果有）
    /// </summary>
    [Column(StringLength = 2000)]
    public string? ErrorMessage { get; set; }

    /// <summary>
    /// 操作开始时间
    /// </summary>
    [Column(IsNullable = false)]
    public DateTime StartTime { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 操作结束时间
    /// </summary>
    public DateTime? EndTime { get; set; }

    /// <summary>
    /// 操作耗时（毫秒）
    /// </summary>
    public int? DurationMs { get; set; }

    /// <summary>
    /// 节点 IP 地址
    /// </summary>
    [Column(StringLength = 100)]
    public string? IpAddress { get; set; }

    /// <summary>
    /// 用户代理（节点客户端版本信息）
    /// </summary>
    [Column(StringLength = 200)]
    public string? UserAgent { get; set; }

    /// <summary>
    /// 日志级别：Debug, Info, Warning, Error
    /// </summary>
    [Column(StringLength = 20, IsNullable = false)]
    public string LogLevel { get; set; } = "Info";

    /// <summary>
    /// 是否已归档
    /// </summary>
    [Column(IsNullable = false)]
    public bool IsArchived { get; set; } = false;

    /// <summary>
    /// 创建时间
    /// </summary>
    [Column(IsNullable = false)]
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 关联的流水线实例
    /// </summary>
    [Navigate(nameof(PipelineInstanceId))]
    public PipelineInstance? PipelineInstance { get; set; }
}
